package net.lesscoding.producer;

import net.lesscoding.common.Const;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author eleven
 * @date 2023/10/18 22:37
 * @apiNote 延迟发送
 */
public class DelayProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("pg");
        producer.setNamesrvAddr(Const.TS_NAME_SRV_ADDR_OS1);
        producer.start();
        Message msg = null;
        for (int i = 0; i < 10; i++) {
            byte[] data = String.format("Hi, %d", i).getBytes();
            msg = new Message("TopicB", "someTag", data);
            // 指定消息延迟登记为3 即延迟10s
            msg.setDelayTimeLevel(3);
            SendResult send = producer.send(msg);
            // 输出消息发送的时间
            System.out.println(new SimpleDateFormat("mm:ss").format(new Date()));
            System.out.println(send);
        }
        producer.shutdown();
    }
}
